package com.jxt.app.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

//import com.danga.MemCached.MemCachedClient;
import com.jxt.app.entity.base.Apiphone;
import com.jxt.app.entity.base.ApiphoneExample;
import com.jxt.app.service.ApiphoneService;
import com.jxt.app.util.MainApnsSend;

@Controller
public class AphoneController {
	@Resource 
	private ApiphoneService apiphoneService;
	//@Resource
	//private MemCachedClient memCachedClient;
	
	private AphoneController(){
		//MainApnsSend.UserOnlineCheck();
	}
	
	/**
	 * 推送TOKEN注册
	 * @param apiphone
	 * @param response
	 * @param requset
	 * @param model
	 * @throws UnsupportedEncodingException
	 */
	 @RequestMapping(value="/app/user/regpush",method=RequestMethod.GET) 
	 public void getDialogId(Apiphone apiphone,HttpServletResponse response,HttpServletRequest requset, Model model) throws UnsupportedEncodingException
	 {
		 
		 	PrintWriter out = null;
			try {
				out = response.getWriter();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
			
			JSONObject obj = new JSONObject();
			Map resultMap=new HashMap();
			 if(null==apiphone){
				 resultMap.put("code", "-1002");
		 		 return;
			 }
			 apiphone.setLogintime(new Date());
			 apiphone.setIsonline((byte)1);
			 apiphone.setIsbanned((byte)0);
			
			ApiphoneExample example=new ApiphoneExample();
			example.setDtoken(apiphone.getDtoken());
			example.setDtoken_Indicator(ApiphoneExample.EXAMPLE_EQUALS);
			List<Apiphone> apiphoneList=apiphoneService.listModel(example);
			
			if(apiphoneList.size()>0){
				
				ApiphoneExample record=new ApiphoneExample();
				record.setDtoken(apiphoneList.get(0).getDtoken());
				record.setUserid(apiphone.getUserid());
				record.setLogintime(new Date());
				record.setIsonline((byte)1);
				record.setIsbanned((byte)0);
				apiphoneService.updateModel(record);
				
				
				Apiphone apiphones=MainApnsSend.apIphoneUser.get(apiphone.getUserid());//更新用户缓存
				if(null==apiphones){
					MainApnsSend.apIphoneUser.put(apiphone.getUserid(), apiphone);
				}
				//apiphones
				String token=UUID.randomUUID().toString().replace("-", "");
				resultMap.put("token",token );
				resultMap.put("code", "1001");//重新注册
				obj.put("result", resultMap);
				out.println(obj.toString());
				
				
				//memCachedClient.set("token", token, new Date(1000*3600*24*1)); //1天过期
				return;
			}
			
			
			int ret= apiphoneService.insertModel(apiphone);
			
			if(ret==1){
				//MainApnsSend.apIphoneUser.put(apiphone.getUserid(), apiphone);
				String token=UUID.randomUUID().toString().replace("-", "");
				obj.put("token", token);
				obj.put("code", "1001");//推送 注册成功
				
				//memCachedClient.set("token", token, new Date(1000*3600*24*1)); //1天过期
			}else{
				 obj.put("code", "-1001");
			}
			 
			out.println(obj.toString());
		 
		 
	 }
	
	
	
	

}
